// pages/search/search.js
const app = getApp()
const util = require("../../utils/util.js");
const api = require("../../utils/api.js");
Page({
    data: {
        scrollTop: {
            scroll_top: 0,
            goTop_show: false
        },
        pageColor: app.pageColor.mainColor,
        searchLabels: true,
        morelabels: [],
        typeNum: null,
        typeName: '',
        goods: {},
        page: 1,
        last_page: null
    },
    scrollTopFun: function (e) {
        var that = this;
        util.scrollTopFun(e, function () {
            that.setData({ 'scrollTop.goTop_show': true })
        }, function () {
            that.setData({ 'scrollTop.goTop_show': false })
        });
    },
    goTopFun: function (e) {
        var that = this;
        var _top = that.data.scrollTop.scroll_top;
        util.goTopFun(_top, function () {
            that.setData({
                'scrollTop.scroll_top': _top
            });
        })
    },
    val:function(e){
        var that =this;
        if (e.detail.value==''){
            that.setData({
                searchLabels:true,
                goods:{},
                page:1
            })
        }
    },
    choseLabel: function (e) {
        var that = this;
        that.setData({
            typeNum: e.currentTarget.dataset.idx,
            typeName: e.currentTarget.dataset.name,
            searchLabels: false
        }, () => {
            that.searchList();
        })
    },
    sureBtn: function (e) {
        var that = this;
        that.setData({
            typeName: e.detail.value,
            searchLabels: false
        }, () => {
            that.searchList();
        })
    },
    searchList: function () {
        var that = this;
        let url = api.search();
        if (that.data.typeName != '') {
            if (that.data.page==1){
                util.http(true, "GET", url, { name: that.data.typeName, page: that.data.page }, res => {
                    that.setData({
                        goods: res.goods,
                        last_page: res.goods.last_page
                    })
                    if(that.data.goods.data.length==0){
                        wx.showModal({
                            showCancel:false,
                            content: '未搜索到相关内容',
                            confirmColor: that.data.pageColor
                        })
                        that.setData({
                            typeName:'',
                            searchLabels:true,
                        })
                    }
                })
            }else{
                if (that.data.page > that.data.last_page) { return }
                util.http(true,"GET", url, { name: that.data.typeName, page: that.data.page }, res => {
                    that.data.goods.data=that.data.goods.data.concat(res.goods.data)
                    that.setData({
                        goods:that.data.goods
                    })
                })
            }
                
        } else {
            wx.showModal({
                showCancel: false,
                content: '请输入您想要搜索的内容',
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.searchRank();
    },
    searchRank: function () {
        var that = this;
        let url = api.searchRank();
        util.http(true, "GET", url, { page: that.data.page }, res => {
            that.setData({
                morelabels: res.rank
            })
        })
    },
    onReady: function () {
        util.setNavigationBarTitle("搜索");
    },
    moreGoods: function () {
        var that = this;
        let a = that.data.page;
        a++
        that.setData({
            page: a
        }, () => {
            that.searchList();
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})